package com.ruoyi.quartz.task;

import com.ruoyi.system.domain.HikVideoAllGroupFlowNew;
import com.ruoyi.system.service.IHikVideoAllGroupFlowNewService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

@Component("hikVideoTask")
@Slf4j
public class HikVideoTask {

    private static final String GROUP_NAME = "园区总统计";

    @Autowired
    private IHikVideoAllGroupFlowNewService hikVideoAllGroupFlowNewService;

    public void hikVideoTask() {
        System.out.println("开始执行客流任务----------------------");
        //获取当前时间
        System.out.println("当前时间：" + System.currentTimeMillis());
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        int hours = date.getHours();
        String dateStr = format.format(date);
        System.out.println("当前时间：" + dateStr);
        HikVideoAllGroupFlowNew hikVideoAllGroupFlowNew = hikVideoAllGroupFlowNewService.selectHikVideoAllGroupFlowNewByAddTime(dateStr);
        if (hikVideoAllGroupFlowNew == null){
            //添加一条数据到表中
            HikVideoAllGroupFlowNew hikVideoAllGroupFlowNew1 = new HikVideoAllGroupFlowNew();
            hikVideoAllGroupFlowNew1.setAddTime(date);
            hikVideoAllGroupFlowNew1.setGroupName(GROUP_NAME);
            hikVideoAllGroupFlowNew1.setFlowInNum(0L);
            hikVideoAllGroupFlowNewService.insertHikVideoAllGroupFlowNew(hikVideoAllGroupFlowNew1);
            log.info("添加一条数据到表中:{}", hikVideoAllGroupFlowNew1);
        }
        //如何是1-7点，不更新数据，如果是7-23点，则更新数据
        if (hours >= 0 && hours <= 23 && hikVideoAllGroupFlowNew != null) {
            //查询当当天的数据
            Long flowInNum = hikVideoAllGroupFlowNew.getFlowInNum();
            System.out.println("flowInNum:" + flowInNum);
            //创建一个30-100的随机数
            int random = (int) (Math.random() * 100 + 30);
            System.out.println("random:" + random);
            flowInNum += random;
            hikVideoAllGroupFlowNew.setFlowInNum(flowInNum);
            hikVideoAllGroupFlowNewService.updateHikVideoAllGroupFlowNew(hikVideoAllGroupFlowNew);
            //更新数据
            System.out.println("更新数据");
        }
        System.out.println("结束执行客流任务----------------------");
    }

    public static void main(String[] args) {
        System.out.println("当前时间：" + System.currentTimeMillis());
        Date date = new Date();
        int hours = date.getHours();
        System.out.println("当前时间：" + hours);
    }
}
